/**
 * 
 */
package pms.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import pms.om.Noten;
import pms.om.Person;
import pms.om.PersonHatNoten;

/** 
 * Controller to generate the Home Page basics to be rendered by a view. 
 * It extends the convenience class AbstractController that encapsulates most 
 * of the drudgery involved in handling HTTP requests. 
/**
 * @author roman.zaglauer@googlemail.com
 *
 */
public class SavePersonHatNoten extends AllePersonHatNoten{
	protected ModelAndView handleRequestInternal(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		httpServletRequest.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt(httpServletRequest.getParameter("id"));
		
		PersonHatNoten phn = getPersonHatNotenDao().findById(id);
		
//		String adatum = httpServletRequest.getParameter("ausleihdatum");
//		String rdatum = httpServletRequest.getParameter("rueckgabedatum");
		
		
//		Date ausleihdatum = new Date(httpServletRequest.getParameter("ausleihdatum"));
//		Date rueckgabedatum = new Date(httpServletRequest.getParameter("rueckgabedatum"));
	
		int notenid = Integer.parseInt(httpServletRequest.getParameter("noten"));
		Noten noten = getPersonHatNotenDao().getNotenDao().findById(notenid);
		
		int personid = Integer.parseInt(httpServletRequest.getParameter("person"));
		Person person = getPersonHatNotenDao().getPersonDao().findById(personid);
		
		if (id == 0) {
			getPersonHatNotenDao().create(new Date(), null, noten, person);
		} else {
			getPersonHatNotenDao().modify(id, phn.getAusleihdatum(), new Date(), noten, person);
		}
		
		return super.handleRequestInternal(httpServletRequest, httpServletResponse);
	}
}
